﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using DoeObjects;
using System.Web.Mvc;

namespace DoeLibsMVC.Models
{
	public class EditTitleModel
	{
        [MinLength(10)]
        [MaxLength(10)]
        [Display(Name = "ISBN-10")]
        public string ISBN10 { get; set; }

        [MinLength(13)]
        [MaxLength(13)]
        [Display(Name = "ISBN-13")]
        public string ISBN13 { get; set; }

        [Required]
        [Display(Name = "Title")]
        public string Title { get; set; }

        [Required]
        [Display(Name = "Edition")]
        public int Edition { get; set; }

        [Required]
        [Display(Name = "Publication year")]
        public int PublicationYear { get; set; }

        [Display(Name = "First edition year")]
        public int? FirstEditionYear { get; set; }

        [Required]
        [Display(Name = "Publisher")]
        public string Publisher 
        {
            get
            {
                if (PublisherObject == null)
                {
                    PublisherObject = new Publisher();
                }
                return PublisherObject.Name;
            }

            set
            {
				if (value == null) return;
                List<Publisher> publishers = DoeObjects.Publisher.getByName(value.Trim(), false);
                Publisher persistPublisher = null;
                if (publishers.Count == 0)
                {
                    //publisher doesn't exists --> create a new one
                    persistPublisher = new Publisher();
                    persistPublisher.Name = value.Trim();
                    persistPublisher.save();
                }
                else
                {
                    //a publisher with this name already exits --> take this name
                    persistPublisher = publishers[0];
                }

                PublisherObject = persistPublisher;
            }
        }

        public Publisher PublisherObject { get; set; }

        [Required]
        [Display(Name = "Authors")]
        public string Authors
        {
            get
            {
                if (AuthorList == null)
                {
                    AuthorList = new List<Author>();
                }

                return string.Join(", ", AuthorList.Select(a => a.Name).ToList());
            }
            set
            {
				if (value == null) return;
                string[] fullnames = null;
                if (value.Contains(","))
                {
                    fullnames = value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                }
                else 
                {
                    fullnames = new string[] {value};
                }

                foreach (string fullname in fullnames)
                {
                    if (!string.IsNullOrEmpty(fullname) && !string.IsNullOrWhiteSpace(fullname))
                    {
                        Author author = null;
                        

                            List<Author> authors = Author.getByName(fullname, false);
                            if (authors.Count > 0)
                            {
                                author = authors[0];
                            }
                            else
                            {
                                author = new Author();
                                author.Name = fullname;
                                author.save();
                            }
                        

                        if (author.Aid > 0)
                        {
                            AuthorList.Add(author);
                        }
                    }
                }
            }
        }

        public List<Author> AuthorList { get; set; }

        [Display(Name = "Editors")]
        public string Editors
        {
            get
            {
                if (EditorList == null)
                {
                    EditorList = new List<Author>();
                }

                return string.Join(", ", EditorList.Select(a => a.Name).ToList());
            }
            set
            {
                if (value == null) return;
                string[] fullnames = null;
                if (value.Contains(","))
                {
                    fullnames = value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                }
                else
                {
                    fullnames = new string[] { value };
                }

                foreach (string fullname in fullnames)
                {
                    if (!string.IsNullOrEmpty(fullname) && !string.IsNullOrWhiteSpace(fullname))
                    {
                        Author author = null;
                        
                            List<Author> authors = Author.getByName(fullname, false);
                            if (authors.Count > 0)
                            {
                                author = authors[0];
                            }
                            else
                            {
                                author = new Author();
                                author.Name = fullname;
                                author.save();
                            }
                        

                        if (author.Aid > 0)
                        {
                            EditorList.Add(author);
                        }
                    }
                }
            }
        }

        public List<Author> EditorList { get; set; }



		[Required]
		[Display(Name = "Topics")]
		public string Topics
		{
			get
			{
				if (TopicList == null)
				{
					TopicList = new List<Topic>();
				}

				return string.Join(", ", TopicList.Select(a => a.TopicName).ToList());
			}
			set
			{
				if (value == null) return;
				string[] fullnames = null;
				if (value.Contains(","))
				{
					fullnames = value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
				}
				else
				{
					fullnames = new string[] { value };
				}

				foreach (string fullname in fullnames)
				{
					if (!string.IsNullOrEmpty(fullname) && !string.IsNullOrWhiteSpace(fullname))
					{
						Topic topic = null;
						//name can be split in firstname and lastname
						//string[] names = fullname.Trim().Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);

						List<Topic> topics = Topic.getByName(fullname.Trim(), false);
						if (topics.Count > 0)
						{
							topic = topics[0];
						}
						else
						{
							topic = new Topic();
							topic.TopicName = fullname.Trim();
							topic.save();
						}
						if (topic.TopicId > 0)
						{
							TopicList.Add(topic);
						}
					}
				}
			}
		}

        public List<Topic> TopicList { get; set; }

        public EditTitleModel()
        {
            AuthorList = new List<Author>();
            EditorList = new List<Author>();
            TopicList = new List<Topic>();
        }
	}
}